/**
 * @date 2016年1月7日 下午3:44:55
 * @version V1.0
 */
package Commons;

import gnu.trove.map.hash.TObjectIntHashMap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @ClassName: get_wiki_word_pairs
 * @Description: TODO
 * @author ffftzh
 * @date 2016年1月7日 下午3:44:55
 *
 */
public class get_wiki_word_pairs {
    HashSet<String> wordSet = new HashSet<String>();
    TObjectIntHashMap<String> wordPairs = new TObjectIntHashMap<String>();
    int Wiki_doc_count = 0;

    public void set_word_pairs() {
        String myW1, myW2;
        for (String word1 : wordSet) {
            for (String word2 : wordSet) {
                myW1 = word1;
                myW2 = word2;
                if (word1.compareTo(word2) > 0) {
                    myW1 = word2;
                    myW2 = word1;
                }
                wordPairs.put(myW1 + "|" + myW2, 0);
            }
        }
    }

    public void read_wiki(String rootPath, String store_path) throws IOException {
        long starttime = System.currentTimeMillis();
        System.out.println("counts:" + wordPairs.size());
        BufferedReader reader = IOUtil.getReader(rootPath);
        String line = null, myW1, myW2, wpitem;
        int lengthAvg = 0;
        while ((line = reader.readLine()) != null) {
            Wiki_doc_count++;
            if (Wiki_doc_count % 1000 == 0) {
                System.out.println(Wiki_doc_count + "\t" + ((System.currentTimeMillis() - starttime) / 1000) + "\t" + (lengthAvg / 1000));
                lengthAvg = 0;
            }
            HashSet<String> oneLineWordMap = new HashSet<String>();
            ArrayList<String> oneLineWordList = new ArrayList<String>();
            String[] lines = line.split("\\s+");
            for (String item : lines) {
                if (oneLineWordMap.contains(item)) {
                    continue;
                }
                oneLineWordMap.add(item);
                if (wordSet.contains(item)) {
                    oneLineWordList.add(item);
                }

            }
            int theLength = oneLineWordList.size();
            lengthAvg += theLength;
            //System.out.println(theLength);
            for (int i1 = 0; i1 < theLength; i1++) {
                for (int i2 = i1; i2 < theLength; i2++) {
                    myW1 = oneLineWordList.get(i1);
                    myW2 = oneLineWordList.get(i2);
                    if (myW1.compareTo(myW2) > 0) {
                        myW1 = oneLineWordList.get(i2);
                        myW2 = oneLineWordList.get(i1);
                    }
                    wpitem = myW1 + "|" + myW2;
                    Integer theStoreOne = wordPairs.get(wpitem);
                    wordPairs.put(wpitem, theStoreOne + 1);
                }
            }
        }

        reader.close();
        System.out.println("counts:" + Wiki_doc_count);

        BufferedWriter writer = IOUtil.getWriter(store_path);
        writer.write(Wiki_doc_count);
        for (Object ones : wordPairs.keys()) {
            writer.write(ones + " " + wordPairs.get(ones) + "\n");
        }
        writer.close();

    }

    public HashMap<Integer, String> readWordMap(String wordMap_path) throws IOException {
        HashMap<Integer, String> result = new HashMap<Integer, String>();
        BufferedReader reader = IOUtil.getReader(wordMap_path);
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] lines = line.split("\\s+");
            if (lines.length != 2) {
                continue;
            }
            result.put(Integer.parseInt(lines[1]), lines[0]);
        }
        reader.close();
        return result;
    }

    public static void main(String[] args) throws IOException {
        String wordMap_path = "";
        String wikiPath = "";
        String store_path = wordMap_path + ".dict";
        get_wiki_word_pairs one = new get_wiki_word_pairs();
        one.readWordMap(wordMap_path);
        one.set_word_pairs();
        one.read_wiki(wikiPath, store_path);

    }

}
